<?php

namespace App\Http\Controllers\Api\Merchant;

use App\AboutUs;
use App\Banner;
use App\Button;
use App\Classify;
use App\Http\Services\FileManager;
use App\Http\Services\MerchantManager;
use App\Http\Services\UserManager;
use App\Merchant;
use App\MerchantMedicine;
use App\MerchantMedicineArea;
use App\MerchantMedicineXArea;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class WxAppController extends Controller
{
    /**
     * banner列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getBannerList(Request $request){
        /** @var User $MerchantUser */
        $MerchantUser = MerchantManager::isLogin($request);
        if(!$MerchantUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $enterpriseInfo = MerchantManager::getMerchantInfoByUserId($MerchantUser->id);
        if(!$enterpriseInfo)
            return response()->json(['level' => 'error','message' => '商家信息异常']);

        $page           = !is_null($request->get('page'))  ? $request->get('page') : 1;
        $limit          = !is_null($request->get('limit')) ? $request->get('limit') : 10;
        $merchantId     = $request->get('merchant_id');

        $page_info=[
            'page'=>$page,
            'limit'=>$limit
        ];

        /** @var Banner $item */
        $banner = Banner::whereNull('deleted_at');

        if($merchantId != 0){
            $banner->whereMerchantId($merchantId);
        }

        $banner = $banner->whereIn('merchant_id',$enterpriseInfo->merchant_id_arr);

        $page_info['total'] = $banner->count();
        $banner = $banner->orderBy('sort','asc')->orderBy('created_at','desc')->offset(($page - 1) * $limit)->limit($limit)->get();
        if (count($banner) > 0){
            foreach ($banner as $item){
                $item->img_url  =FileManager::getImgUrl($item->img_id);

                $item->merchant_name = Merchant::getMerchantName($item->merchant_id);
            }
        }

        return response()->json(['level'=>'success','message'=>'获取成功','page_info'=>$page_info,'data'=>$banner]);

    }

    /**
     * banner详情
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getBannerDetail(Request $request){
        /** @var User $MerchantUser */
        $MerchantUser = MerchantManager::isLogin($request);
        if(!$MerchantUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $id=$request->get('id');

        $banner=Banner::whereId($id)->first();
        if ($banner){
            $banner->img_url=FileManager::getImgUrl($banner->img_id);

            $banner->merchant_name = Merchant::getMerchantName($banner->merchant_id);
        }

        return response()->json(['level'=>'success','message'=>'获取成功','data'=>$banner]);
    }

    /**
     * 设置banner
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function setBanner(Request $request){
        /** @var User $MerchantUser */
        $MerchantUser = MerchantManager::isLogin($request);
        if(!$MerchantUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $id             = $request->get('id');
        $merchantId     = $request->get('merchant_id');
        $img_id =$request->get('img_id');
        $title  =$request->get('title');
        $link_url =$request->get('link_url');
        $sort   =$request->get('sort');
        $status =$request->get('status');

        $merchant = Merchant::whereId($merchantId)->first();
        if(!$merchant)
            return response()->json(['level' => 'error','message' => '请选择门店']);

        $merchant = Merchant::whereId($merchantId)->first();
        if(!$merchant)
            return response()->json(['level' => 'error','message' => '门店不存在']);

        $banner=Banner::whereId($id)->first();
        if (!$banner)
            $banner=new Banner();

        $banner->merchant_id = $merchantId;
        if ($title)
            $banner->title=$title;
        if ($img_id)
            $banner->img_id =$img_id;
        if ($link_url)
            $banner->link_url=$link_url;
        if ($sort)
            $banner->sort   =$sort;
        $banner->status =1;
        $banner->location ='index';
        $banner->save();
        return response()->json(['level'=>'success','message'=>'操作成功','data'=>$banner]);
    }

    /**
     * 删除banner
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function delBanner(Request $request){
        /** @var User $MerchantUser */
        $MerchantUser = MerchantManager::isLogin($request);
        if(!$MerchantUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $id     =$request->get('id');

        $banner=Banner::whereId($id)->first();
        if (!$banner)
            return response()->json(['level'=>'error','message'=>'banner不存在']);

        $banner->delete();

        return response()->json(['level'=>'success','message'=>'删除成功']);
    }

    /**
     * 功能区列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getButtonList(Request $request){
        /** @var User $MerchantUser */
        $MerchantUser = MerchantManager::isLogin($request);
        if(!$MerchantUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $enterpriseInfo = MerchantManager::getMerchantInfoByUserId($MerchantUser->id);
        if(!$enterpriseInfo)
            return response()->json(['level' => 'error','message' => '商家信息异常']);

        $page       =!is_null($request->get('page'))  ? $request->get('page') : 1;
        $limit      =!is_null($request->get('limit')) ? $request->get('limit') : 10;
        $merchantId = $request->get('merchant_id') ?? 0;

        $page_info=[
            'page'=>$page,
            'limit'=>$limit
        ];

        /** @var Button $button */
        $buttonList = Button::whereNull('deleted_at');

        if($merchantId != 0){
            $buttonList->whereMerchantId($merchantId);
        }

        $buttonList = $buttonList->whereIn('merchant_id',$enterpriseInfo->merchant_id_arr);

        $page_info['total'] = $buttonList->count();

        $buttonList = $buttonList->orderBy('created_at','desc')->offset(($page - 1) * $limit)->limit($limit)->get();
        if(count($buttonList) > 0){
            foreach ($buttonList as $button) {
                $button->img_url = FileManager::getImgUrl($button->img_id);

                $button->classify_name = Classify::getClassifyName($button->classify_id);

                $button->merchant_name = Merchant::getMerchantName($button->merchant_id);
            }
        }

        return response()->json(['level' => 'success','message' => '获取成功','data' => $buttonList,'page_info' => $page_info]);
    }

    /**
     * 功能区详情
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getButtonDetail(Request $request){
        /** @var User $MerchantUser */
        $MerchantUser = MerchantManager::isLogin($request);
        if(!$MerchantUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $id = $request->get('id') ?? 0;

        $button = Button::whereId($id)->first();
        if($button){
            $button->img_url = FileManager::getImgUrl($button->img_id);

            $button->merchant_name = Merchant::getMerchantName($button->merchant_id);

            $button->parent_classify_id = 0;
            $classify = Classify::whereId($button->classify_id)->first();
            if($classify){
                $parentClassify = Classify::whereId($classify->pid)->first();
                if($parentClassify){
                    $button->parent_classify_id = $parentClassify->id;
                }
            }
        }

        return response()->json(['level' => 'success','message' => '获取成功','data' => $button]);
    }

    /**
     * 设置功能区
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function setButton(Request $request){
        /** @var User $MerchantUser */
        $MerchantUser = MerchantManager::isLogin($request);
        if(!$MerchantUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $enterpriseInfo = MerchantManager::getMerchantInfoByUserId($MerchantUser->id);
        if(!$enterpriseInfo)
            return response()->json(['level' => 'error','message' => '商家信息异常']);

        $merchantId = $request->get('merchant_id') ?? 0;
        $title      = $request->get('title') ?? '';
        $imgId      = $request->get('img_id') ?? 0;
        $linkUrl    = $request->get('link_url') ?? '';
        $sort       = $request->get('sort') ?? 0;
        $classifyId = $request->get('classify_id') ?? 0;
        $id         = $request->get('id') ?? 0;

        $merchant = Merchant::whereId($merchantId)->first();
        if(!$merchant)
            return response()->json(['level' => 'error','message' => '请选择门店']);

        $button = Button::whereId($id)->first();
        if(!$button){
            $buttonCount = Button::whereMerchantId($merchantId)->count();
            if($buttonCount >= 10)
                return response()->json(['level' => 'error','message' => '已达到最大数量']);

            $button = new Button();
        }

        $button->merchant_id         = $merchantId;
        $button->classify_id         = $classifyId;
        $button->title               = $title;
        $button->img_id              = $imgId;
        $button->link_url            = $linkUrl;
        $button->sort                = $sort;
        $button->status              = 1;
        $button->location            = 'index';
        $button->save();

        return response()->json(['level' => 'success','message' => '设置成功','data' => $button]);
    }

    /**
     * 删除功能区
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function delButton(Request $request){
        /** @var User $MerchantUser */
        $MerchantUser = MerchantManager::isLogin($request);
        if(!$MerchantUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $id = $request->get('id') ?? 0;

        $button = Button::whereId($id)->first();
        if(!$button)
            return response()->json(['level' => 'error','message' => '删除失败']);

        $button->delete();

        return response()->json(['level' => 'success','message' => '删除成功','data' => $button]);
    }

    /**
     * 专区列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getMerchantMedicineAreaList(Request $request){
        /** @var User $MerchantUser */
        $MerchantUser = MerchantManager::isLogin($request);
        if(!$MerchantUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $enterpriseInfo = MerchantManager::getMerchantInfoByUserId($MerchantUser->id);
        if(!$enterpriseInfo)
            return response()->json(['level' => 'error','message' => '商家信息异常']);

        $page       =!is_null($request->get('page'))  ? $request->get('page') : 1;
        $limit      =!is_null($request->get('limit')) ? $request->get('limit') : 10;
        $merchantId = $request->get('merchant_id') ?? 0;

        $page_info=[
            'page'=>$page,
            'limit'=>$limit
        ];

        /** @var MerchantMedicineArea $merchantMedicineArea */
        $merchantMedicineAreaList = MerchantMedicineArea::whereNull('deleted_at');

        if($merchantId != 0){
            $merchantMedicineAreaList->whereMerchantId($merchantId);
        }

        $merchantMedicineAreaList = $merchantMedicineAreaList->whereIn('merchant_id',$enterpriseInfo->merchant_id_arr);

        $page_info['total'] = $merchantMedicineAreaList->count();

        $merchantMedicineAreaList = $merchantMedicineAreaList->orderBy('sort','asc')->orderBy('created_at','desc')->offset(($page - 1) * $limit)->limit($limit)->get();
        if(count($merchantMedicineAreaList) > 0){
            foreach ($merchantMedicineAreaList as $merchantMedicineArea) {
                $merchantMedicineArea->img_uel = FileManager::getImgUrl($merchantMedicineArea->img_id);

                $merchantMedicineArea->merchant_name = Merchant::getMerchantName($merchantMedicineArea->merchant_id);
            }
        }

        return response()->json(['level' => 'success','message' => '获取成功','data' => $merchantMedicineAreaList,'page_info' => $page_info]);
    }

    /**
     * 专区详情
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getMerchantMedicineAreaDetail(Request $request){
        /** @var User $MerchantUser */
        $MerchantUser = MerchantManager::isLogin($request);
        if(!$MerchantUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $id = $request->get('id') ?? 0;

        $merchantMedicineArea = MerchantMedicineArea::whereId($id)->first();
        if($merchantMedicineArea){
            $merchantMedicineArea->img_url = FileManager::getImgUrl($merchantMedicineArea->img_id);

            $merchantMedicineArea->merchant_name = Merchant::getMerchantName($merchantMedicineArea->merchant_id);
        }

        return response()->json(['level' => 'success','message' => '获取成功','data' => $merchantMedicineArea]);
    }

    /**
     * 设置专区
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function setMerchantMedicineArea(Request $request){
        /** @var User $MerchantUser */
        $MerchantUser = MerchantManager::isLogin($request);
        if(!$MerchantUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $merchantId = $request->get('merchant_id') ?? 0;
        $name       = $request->get('name') ?? '';
        $imgId      = $request->get('img_id') ?? 0;
        $sort       = $request->get('sort') ?? 0;
        $id         = $request->get('id') ?? 0;

        $merchant = Merchant::whereId($merchantId)->first();
        if(!$merchant)
            return response()->json(['level' => 'error','message' => '请选择门店']);

        $merchantMedicineArea = MerchantMedicineArea::whereId($id)->first();
        if(!$merchantMedicineArea)
            $merchantMedicineArea = new MerchantMedicineArea();

        $merchantMedicineArea->merchant_id        = $merchantId;
        $merchantMedicineArea->name               = $name;
        $merchantMedicineArea->img_id             = $imgId;
        $merchantMedicineArea->sort               = $sort;
        $merchantMedicineArea->status             = 1;
        $merchantMedicineArea->save();

        return response()->json(['level' => 'success','message' => '设置成功','data' => $merchantMedicineArea]);
    }

    /**
     * 删除专区
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function delMerchantMedicineArea(Request $request){
        /** @var User $MerchantUser */
        $MerchantUser = MerchantManager::isLogin($request);
        if(!$MerchantUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $id = $request->get('id') ?? 0;

        $merchantMedicineArea = MerchantMedicineArea::whereId($id)->first();
        if(!$merchantMedicineArea)
            return response()->json(['level' => 'error','message' => '删除失败']);

        $merchantMedicineArea->delete();

        return response()->json(['level' => 'success','message' => '删除成功','data' => $merchantMedicineArea]);
    }

    /**
     * 专区绑定的商品列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getMerchantMedicineXAreaList(Request $request){
        /** @var User $MerchantUser */
        $MerchantUser = MerchantManager::isLogin($request);
        if(!$MerchantUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $page       =!is_null($request->get('page'))  ? $request->get('page') : 1;
        $limit      =!is_null($request->get('limit')) ? $request->get('limit') : 10;
        $id         = $request->get('merchant_medicine_area_id') ?? 0;

        $page_info=[
            'page'=>$page,
            'limit'=>$limit
        ];

        /** @var MerchantMedicineXArea $merchantMedicineXArea */
        $merchantMedicineXAreaList = MerchantMedicineXArea::whereMerchantMedicineAreaId($id);

        $page_info['total'] = $merchantMedicineXAreaList->count();

        $merchantMedicineXAreaList = $merchantMedicineXAreaList->orderByDesc('created_at')->offset(($page - 1) * $limit)->limit($limit)->get();
        if(count($merchantMedicineXAreaList) > 0){
            foreach ($merchantMedicineXAreaList as $merchantMedicineXArea) {
                $goods = MerchantMedicine::getMedicineListItemInfo($merchantMedicineXArea->merchant_medicine_id);

                $merchantMedicineXArea->goods_name = $goods->name ?? '';
                $merchantMedicineXArea->goods_thumb_url = $goods->thumb_url ?? '';
            }
        }

        return response()->json(['level' => 'success','message' => '获取成功','data' => $merchantMedicineXAreaList,'page_info' => $page_info]);
    }

    /**
     * 专区绑定商品
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function setMerchantMedicineXArea(Request $request){
        /** @var User $MerchantUser */
        $MerchantUser = MerchantManager::isLogin($request);
        if(!$MerchantUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $merchantMedicineId = $request->get('merchant_medicine_id') ?? 0;
        $id                 = $request->get('merchant_medicine_area_id') ?? 0;

        $merchantMedicineXArea = MerchantMedicineXArea::whereMerchantMedicineId($merchantMedicineId)->whereMerchantMedicineAreaId($id)->first();
        if($merchantMedicineXArea)
            return response()->json(['level' => 'error','message' => '已绑定过该商品']);

        $merchantMedicineXArea = new MerchantMedicineXArea();
        $merchantMedicineXArea->merchant_medicine_id        = $merchantMedicineId;
        $merchantMedicineXArea->merchant_medicine_area_id   = $id;
        $merchantMedicineXArea->save();

        return response()->json(['level' => 'success','message' => '设置成功','data' => $merchantMedicineXArea]);
    }

    /**
     * 删除专区绑定的商品
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function delMerchantMedicineXArea(Request $request){
        /** @var User $MerchantUser */
        $MerchantUser = MerchantManager::isLogin($request);
        if(!$MerchantUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $id = $request->get('id') ?? 0;

        $merchantMedicineXArea = MerchantMedicineXArea::whereId($id)->first();
        if(!$merchantMedicineXArea)
            return response()->json(['level' => 'error','message' => '删除失败']);

        $merchantMedicineXArea->delete();

        return response()->json(['level' => 'success','message' => '删除成功','data' => $merchantMedicineXArea]);
    }

    /**
     * 获取关于我们
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getAboutUs(Request $request){
        /** @var User $MerchantUser */
        $MerchantUser = MerchantManager::isLogin($request);
        if(!$MerchantUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $enterpriseInfo = MerchantManager::getMerchantInfoByUserId($MerchantUser->id);
        if(!$enterpriseInfo)
            return response()->json(['level' => 'error','message' => '商家信息异常']);

        $aboutUs = AboutUs::whereEnterpriseId($enterpriseInfo->id)->whereType('about_us')->first();

        return response()->json(['level' => 'success','message' => '获取成功','data' => $aboutUs]);
    }

    /**
     * 设置关于我们
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function setAboutUs(Request $request){
        /** @var User $MerchantUser */
        $MerchantUser = MerchantManager::isLogin($request);
        if(!$MerchantUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $enterpriseInfo = MerchantManager::getMerchantInfoByUserId($MerchantUser->id);
        if(!$enterpriseInfo)
            return response()->json(['level' => 'error','message' => '商家信息异常']);

        $detail = $request->get('detail') ?? '';

        $aboutUs = AboutUs::whereEnterpriseId($enterpriseInfo->id)->whereType('about_us')->first();
        if(!$aboutUs)
            $aboutUs = new AboutUs();

        $aboutUs->enterprise_id          = $enterpriseInfo->id;
        $aboutUs->type                   = 'about_us';
        $aboutUs->detail                 = $detail;
        $aboutUs->save();

        return response()->json(['level' => 'success','message' => '设置成功','data' => $aboutUs]);
    }
}
